﻿using System;
using System.Collections.Generic;
using Ping.App.Jobs;
using Ping.Core;

namespace Ping.App
{
	public delegate Job CreateJobHandler(Connection connection);

	public class Scheduler
	{
		private readonly Connection _connection;
		private readonly Dictionary<Type, CreateJobHandler> _jobs;

		public Scheduler(Connection connection)
		{
			_connection = connection;

			_jobs = new Dictionary<Type, CreateJobHandler>();
			_jobs[typeof (DnsRequest)] = c => new DnsJob(c);
			_jobs[typeof(PingRequest)] = c => new PingJob(c);
			_jobs[typeof(TracerouteRequest)] = c => new TracerouteJob(c);
			_jobs[typeof (GeoRequest)] = c => new GeoJob(c);
		}

		public Job GetJob(Request request)
		{
			if (request == null)
				throw new ArgumentNullException();

			CreateJobHandler handler;
			if (!_jobs.TryGetValue(request.GetType(), out handler))
				throw new ArgumentException(string.Format("Unknown request type: {0}", request.GetType()));

			return handler(_connection);
		}
	}
}